﻿var sql = require( 'node-sqlserver' );
var dbContext = require( "../DataAccess/dbContext" );

/*
 * 获取休假类型
 */

exports.GetLeaveTypeList = function ( successCall ) {

    dbContext.openHRConn( function ( conn ) {

        conn.query( "select * from aLeaveType ", function ( err, results ) {

            if ( err ) {
                console.log( "Select in DB Error: " + err );
            }

            if ( typeof successCall == "function" ) {
                successCall( results );
            }

        });
    });
};

/*
 * 根据ID获取休假类型
 */
exports.GetLeaveTypeById = function ( id, successCall ) {

    dbContext.openHRConn( function ( conn ) {

        conn.query( "select * from aLeaveType where Id= ?", [id], function ( err, rows ) {

            if ( err ) {
                console.log( "Select in DB Error: " + err );
            }

            var result = null;

            if ( rows != null && rows.length > 0 ) {
                result = rows[0];
            }

            if ( typeof successCall == "function" ) {
                successCall( result );
            }
        });
    });
};

/*
 * 插入操作日志
 */
exports.insertApplicationAction = function ( applicationAction, successCall ){
    
    dbContext.openHRConn( function ( conn ) {
        
        conn.query(
            "insert into ApplicationAction(ApplicationID,Node,Action,ActionUserID,ActionUserName,Comment,ActionTime) values(?,?,?,?,?,?,?)",
            [
                applicationAction.ApplicationID,
                applicationAction.Node,
                applicationAction.Action,
                applicationAction.ActionUserID || 0,
                applicationAction.ActionUserName || '',
                applicationAction.Comment,
                new Date()
            ],
            function ( err, rows ) {
                if ( err ) {

                    err = "insertApplicatioAction:" + err;
                    console.log( "Select in DB Error: " + err );
                }

                successCall( err, rows );
            });
            
    });

}
